﻿using System.IO;
using System.Reflection;
using System.Windows.Threading;
using DesignPattern.Services;
using DesignPattern.ViewModels.Pages;
using DesignPattern.ViewModels.Windows;
using DesignPattern.Views.Pages;
using DesignPattern.Views.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Wpf.Ui;
using Wpf.Ui.DependencyInjection;

//使用通过主机host构建服务
namespace DesignPattern
{
    public partial class App
    {
        private static readonly IHost _host = Host
            .CreateDefaultBuilder()
            .ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(AppContext.BaseDirectory)); })
            .ConfigureServices((context, services) =>
            {
                // 添加导航视图页面提供程序，用于管理导航视图中的页面
                services.AddNavigationViewPageProvider();
                // 添加ApplicationHostService作为托管服务(自动启动)，负责应用程序的启动和生命周期管理
                // 该服务控制应用程序的初始化和主窗口的显示
                services.AddHostedService<ApplicationHostService>();
                // 注册主题管理服务
                services.AddSingleton<IThemeService, ThemeService>();
                // 注册内容对话框服务
                services.AddSingleton<IContentDialogService, ContentDialogService>();
                // 注册消息通知服务
                // 服务与在mainwindow占位的控件关联后可以在任意地方通过show方法显示消息
                services.AddSingleton<ISnackbarService, SnackbarService>();
                // 注册任务栏服务
                // 允许应用程序控制windows任务栏上的应用图标状态、进度条和缩略图按钮
                services.AddSingleton<ITaskBarService, TaskBarService>();
                // 注册导航服务
                services.AddSingleton<INavigationService, NavigationService>();
                // 导航主窗口
                services.AddSingleton<INavigationWindow, MainWindow>();
                // 添加设计模式服务
                services.AddSingleton<DesignPatternService>();
                services.AddSingleton<MainWindowViewModel>();
                // 页面和视图模型
                services.AddSingleton<DashboardPage>();
                services.AddSingleton<DashboardViewModel>();
                services.AddSingleton<SettingsPage>();
                services.AddSingleton<SettingsViewModel>();
                // 注册DesignPatternViewModel为单例，确保所有页面共享同一个实例
                services.AddSingleton<DesignPatternViewModel>();
                // 注册DesignPatternPage为瞬态，每次导航都创建新的页面实例
                services.AddTransient<DesignPatternPage>();
                services.AddTransient<CreationalPage>();
                services.AddTransient<CreationalViewModel>();
                services.AddTransient<StructuralPage>();
                services.AddTransient<StructuralViewModel>();
                services.AddTransient<BehavioralPage>();
                services.AddTransient<BehavioralViewModel>();
            }).Build();
        //外部可以获取容器App.Services.GetRequiredService<T>
        public static IServiceProvider Services
        {
            get { return _host.Services; }
        }
        private async void OnStartup(object sender, StartupEventArgs e)
        {
            await _host.StartAsync();
        }
        private async void OnExit(object sender, ExitEventArgs e)
        {
            await _host.StopAsync();
            _host.Dispose();
        }
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
        }
    }
}